import { validatePwd, downLoadUrl } from "@/services";
import util from "@/utils";
import moment from "moment";

export default {
  data() {
    return {
      zippasswd: "",
      singleStep: 0,
      batchStep: 0,
      isSingleExportShow: false,
      isBatchExportShow: false,
      pageNumber: 1,
      qry_len: 50,
      total_num: 0,
      cardList: [],
      checkedAll: false,
      isIndeterminate: false,
      formData: {
        channel: void 0,
        start_time: moment()
          .startOf("day")
          .subtract(7, "days")
          .format("YYYY-MM-DD HH:mm:ss"),
        stop_time: moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
      },
      isDetailShow: false,
      row: {},
    };
  },
  computed: {
    selectedList() {
      return this.cardList.filter((item) => item.isSelect);
    },
    start_no: {
      get() {
        return 1 + this.qry_len * (this.pageNumber - 1);
      },
    },
  },
  watch: {
    cardList: {
      handler(val) {
        if (val.length === 0) return;
        let allChecked = true;
        let allNoChecked = true;
        val.forEach((item) => {
          if (!item.isSelect) {
            allChecked = false;
          } else {
            allNoChecked = false;
          }
        });
        this.checkedAll = allChecked;
        this.isIndeterminate = !allNoChecked && !allChecked;
      },
      deep: true,
    },
  },
  mounted() {
    this.search();
  },
  methods: {
    search() {
      this.pageNumber = 1;
      this.getData();
    },
    async getData() {
      const params = {
        ...this.formData,
        channels:
        (this.formData.channels && this.formData.channels.join(",")) ||
        void 0,
        start_no: this.start_no,
        qry_len: this.qry_len,
      };
      const res = await this.getList(params);
      if (res.code == 0) {
        this.cardList = res.data.record;
        this.total_num = res.data.total_num;
      }
    },
    async confirmSingle(form) {
      if (this.singleStep === 1) {
        this.isSingleExportShow = false;
        this.zippasswd = "";
        this.singleStep = 0;
        return;
      } else {
        const { curuser_pwd } = form;
        const res = await validatePwd({ userpasswd: curuser_pwd });
        if (res.code === 0) {
          const params = {
            snap_ids: this.selectedList.map((item) => item.snap_id).join(","),
          };
          const res = await this.exportSingle(params);
          this.singleStep = 1;
          this.zippasswd = res.data.zippasswd;
          downLoadUrl(res.data.export_url);
        }
      }
    },
    async confirmBatch(form) {
      // 验证密码
      if (this.batchStep === 0) {
        const { curuser_pwd } = form;
        const res = await validatePwd({ userpasswd: curuser_pwd });
        if (res.code === 0) {
          this.batchStep = 1;
        }
      } else {
        const params = {
          ...form,
        };
        const res = await this.exportBatch(params);
        if (res.code === 0) {
          this.zippasswd = res.data.zippasswd;
          downLoadUrl(res.data.export_url);
        }
      }
    },
    handleExport() {
      if (!this.selectedList.length) {
        util.msg.warning("selectPromit");
        return;
      }
      this.isSingleExportShow = true;
    },
    cancelSingle() {
      this.singleStep = 0;
      this.zippasswd = "";
    },
    cancelBatch() {
      this.batchStep = 0;
      this.zippasswd = "";
    },
    handleSelect(cardInfo) {
      this.$set(cardInfo, "isSelect", !cardInfo.isSelect);
    },
    handleSelectAll(val) {
      this.cardList.forEach((item) => {
        this.$set(item, "isSelect", val);
      });
    },
    handleShowDetail(item) {
      this.isDetailShow = true;
      this.row = { ...item };
    },
    handleExportAll() {
      // 全部导出
      this.isBatchExportShow = true;
    },
    handleDeleteMultiple() {
      if (!this.selectedList.length) {
        this.$message.warning(this.$t("common.notSelectRecord"));
        return;
      }
      this.$confirm(
        this.$t("common.currentDeleteRecord", [this.selectedList.length]),
        this.$t("common.multipleDelete"),
        {
          confirmButtonText: this.$t("common.confirmQueding"),
          cancelButtonText: this.$t("common.cancel"),
          type: "warning",
        }
      ).then(() => {
        this.deleteCb();
      });
    },
    async deleteCb() {
      const params = {
        catch_ids: this.selectedList.map((item) => item.snap_id).join(","),
        type: 2,
      };
      const res = await this.deleteRecord(params);
      if (res.code === 0) {
        util.msg.success("deleteSuccess");
        this.getData();
      }
    },
  },
};
